package com.fingard.dsp.bank.directbank.icbc02;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalanceCheckConfirm;
import com.fingard.dsp.bank.reqrespobj.balancecheckconfirm.ReqBalCheckConfirmQry;
import com.fingard.dsp.bank.reqrespobj.balancecheckconfirm.RetAtsBalCheckConfirmQry;
import com.fingard.dsp.bank.reqrespobj.balancecheckconfirm.detail.RetAtsBalCheckConfirmDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by Lewis on 2017/6/5.
 */
public class ICBC02BalanceCheckConfirm extends ICBC02Base implements IBalanceCheckConfirm {
    //单个余额对账查询和批量余额对账查询，统一采用流处理方式
    //1、循环调用ReqBalCheckConfirmQry.readRecord()读取一个请求明细，然后写入发银行的XML对象
    //2、发银行
    //3、循环调用XmlTextReader.read()读取一个银行返回节点，赋值返回ATS的明细对象，
    //   然后调用RetAtsBalCheckConfirmQry.writeRetDetail()，写入返回ATS的明细
    @Override
    public RetAtsBalCheckConfirmQry d_7509_BalCheckConfirm(ReqBalCheckConfirmQry p_req7509) throws Exception {
        //余额对账查询返回ATS的对象
        RetAtsBalCheckConfirmQry retAts = new RetAtsBalCheckConfirmQry(p_req7509.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            bnkPackageID = p_req7509.reqHeader.transSeqID;
            bnkTransCode = "YDZSUBBDZ";
            bnkfSeqno = bnkPackageID;
            bnkVersion = "0.0.1.0";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入ICBC02通用的头部节点
            writeCommonHeader(xmlWriter);
            xmlWriter.writeElementString("TotalNum", String.valueOf(p_req7509.detailCount));
            xmlWriter.writeElementString("SignTime", Format.DateTime17Format.format(new Date()));
            xmlWriter.writeElementClosed("ReqReserved1");
            xmlWriter.writeElementClosed("ReqReserved2");//两个备用字段
            int rdCount = 0;
            //模拟动态账号
            //List<String> list = new ArrayList<String>();
            while (p_req7509.readRecord()) {
                //模拟动态账号
                /*String account =  p_req7509.readedDetail.actNum;
                list.add(account);*/
                //模拟动态账号
                rdCount++;
                xmlWriter.writeStartElement("rd");
                xmlWriter.writeElementString("iSeqno", String.valueOf(rdCount));
                xmlWriter.writeElementString("AccNo", p_req7509.readedDetail.actNum);
                String creditNo = p_req7509.readedDetail.creditNo;
                String depositNo = p_req7509.readedDetail.depositNo;
                String loanNo = "";
                String dzFlag = p_req7509.readedDetail.dzFlag;
                if (!"".equals(creditNo) && creditNo != null) {
                    loanNo = creditNo;
                } else {
                    loanNo = depositNo;
                }
                if ("1".equals(dzFlag)) {
                    dzFlag = "3";
                } else if ("2".equals(dzFlag)) {
                    dzFlag = "1";
                } else {
                    dzFlag = "2";
                }
                xmlWriter.writeElementString("LoanNo", loanNo);
                xmlWriter.writeElementString("DZDNo", p_req7509.readedDetail.statementNumber);
                xmlWriter.writeElementString("DZFlag", dzFlag);
                xmlWriter.writeElementClosed("ReqReserved3");
                xmlWriter.writeElementClosed("ReqReserved4");
                if (!"".equals(p_req7509.readedDetail.actCur)) {
                    xmlWriter.writeElementString("CurrType", convertCurToBank(p_req7509.readedDetail.actCur));
                }
                xmlWriter.writeEndElement();
            }
            xmlWriter.writeEndElement();
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();//CMS
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), true);
//            String[] tmpStrRet = new String[]{"", "<?xml  version=\"1.0\" encoding=\"GBK\" ?>\n" +
//                    "<CMS>\n" +
//                    "<eb>\n" +
//                    "<pub>\n" +
//                    "<TransCode>YDZSUBBDZ</TransCode>\n" +
//                    "<CIS>020090005542708</CIS>\n" +
//                    "<BankCode>102</BankCode>\n" +
//                    "<ID>JRTT.y.0200</ID>\n" +
//                    "<TranDate>20200403</TranDate>\n" +
//                    "<TranTime>113257935</TranTime>\n" +
//                    "<fSeqno>1133001190</fSeqno>\n" +
//                    "<SerialNo></SerialNo>\n" +
//                    "<RetCode>0</RetCode>\n" +
//                    "<RetMsg></RetMsg>\n" +
//                    "</pub>\n" +
//                    "<out>\n" +
//                    "<TotalNum>1</TotalNum>\n" +
//                    "<RepReserved1></RepReserved1>\n" +
//                    "<RepReserved2></RepReserved2>\n" +
//                    "<rd>\n" +
//                    "<iSeqno>1</iSeqno>\n" +
//                    "<OrderNo>1</OrderNo>\n" +
//                    "<AccNo>3901210009200011932</AccNo>\n" +
//                    "<LoanNo>00000000000000000</LoanNo>\n" +
//                    "<DZDNo>390100002020033130034348</DZDNo>\n" +
//                    "<DZFlag>1</DZFlag>\n" +
//                    "<Result>0</Result>\n" +
//                    "<iRetCode></iRetCode>\n" +
//                    "<iRetMsg></iRetMsg>\n" +
//                    "<RepReserved3></RepReserved3>\n" +
//                    "<RepReserved4></RepReserved4>\n" +
//                    "<CurrType></CurrType>\n" +
//                    "</rd>\n" +
//                    "</out>\n" +
//                    "</eb>\n" +
//                    "</CMS>"};
            //模拟银行返回报文
            /*tmpStrRet[0]="";
            String c = "";
            String a = new String();
            a = "<?xml version=\"1.0\" encoding = \"GB2312\"?>\n" +
                    "\t<CMS>\n" +
                    "\t\t<eb>\n" +
                    "\t\t\t<pub>\n" +
                    "\t\t\t\t<TransCode>交易代码</TransCode>\n" +
                    "\t\t\t\t<CIS>集团CIS号</CIS>\n" +
                    "\t\t\t\t<BankCode>归属银行编号</BankCode>\n" +
                    "\t\t\t\t<ID>证书ID</ID>\t\n" +
                    "\t\t\t\t<TranDate>201706</TranDate>\n" +
                    "\t\t\t\t<TranTime>1756892</TranTime>\n" +
                    "\t\t\t\t<fSeqno>123784</fSeqno>\n" +
                    "<SerialNo>22</SerialNo>\n" +
                    "\t\t\t\t<RetCode>00000</RetCode>\n" +
                    "\t\t\t\t<RetMsg>交易返回描述</RetMsg>\n" +
                    "\t\t\t</pub>\n" +
                    "\t\t\t<out>\n" +
                    "\t\t\t\t<TotalNum>1</TotalNum>\n" +
                    "<RepReserved1>响应备用字段1</RepReserved1>\n" +
                    "<RepReserved2>响应备用字段2</RepReserved2>\t\n" ;
            for(int i=0;i<rdCount;i++) {
                String b = "\t\t\t\t<rd>\n" +
                        "\t\t\t\t\t<iSeqno>"+i+1+"</iSeqno>\n" +
                        "\t\t\t\t\t<OrderNo>平台交易序号</OrderNo>\n" +
                        "\t<AccNo>" + list.get(i) + "</AccNo>\n" +
                        "\t<LoanNo>1234</LoanNo>\n" +
                        "\t<DZDNo>456</DZDNo>\n" +
                        "\t<DZFlag>1</DZFlag>\t\n" +
                        "<Result>返回成功</Result>\n" +
                        "\t\t\t\t\t<iRetCode>0</iRetCode>\n" +
                        "<iRetMsg>明细交易返回描述</iRetMsg>\n" +
                        "<RepReserved3>响应备用字段3</RepReserved3>\n" +
                        "<RepReserved4>响应备用字段4</RepReserved4>\t\n" +
                        "<CurrType>1</CurrType>\n" +
                        "\t\t\t\t</rd>\n" ;
                 c=c+b;
            }
            String d = "\t\t\t</out>\n" +
                    "\t\t</eb>\n" +
                    "</CMS>\n";
            tmpStrRet[1] = a+c+d;*/
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();

                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
                    if (isActiveRetCode(tmpRetCode)) {
                        while (tmpBkReader.read("/CMS/eb/out/rd")) {
                            RetAtsBalCheckConfirmDetail tmpRetDetail = new RetAtsBalCheckConfirmDetail();
//                            if (tmpBkReader.readedXElement.getSingleText("iRetCode").equals("")) {//不知道做不做为判断标志
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.respInfo = tmpBkReader.readedXElement.getSingleText("iRetMsg");
                            /*tmpRetDetail.iSeqno = tmpBkReader.readedXElement.getSingleText("iSeqno");//指令顺序号*/
                            tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("AccNo");
                            tmpRetDetail.loanNo = tmpBkReader.readedXElement.getSingleText("LoanNo");
                            tmpRetDetail.statementNumber = tmpBkReader.readedXElement.getSingleText("DZDNo");
                            String dzFlag = tmpBkReader.readedXElement.getSingleText("Result");
                            if ("0".equals(dzFlag)) {
                                dzFlag = "2";
                            } else if ("1".equals(dzFlag)) {
                                dzFlag = "1";
                            }
                            tmpRetDetail.dzFlag = dzFlag;
                            tmpRetDetail.memo = tmpBkReader.readedXElement.getSingleText("Result");
                            tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("CurrType"));
//                            } else {
//                                tmpRetDetail.respCode = "-1";
//                                tmpRetDetail.respInfo = tmpBkReader.readedXElement.getSingleText("iRetMsg");
//                            }
                            retAts.writeRetDetail(tmpRetDetail, p_req7509.isBatch);
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        if (tmpBkReader.getSimpleText("iRetMsg") != null && tmpBkReader.getSimpleText("iRetMsg").length() > 0) {
                            retAts.writeRespInfo(tmpBkReader.getSimpleText("iRetMsg"));
                        } else {
                            retAts.writeRespInfo(tmpBkReader.getSimpleText("RetMsg"));
                        }
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpStrRet[1]);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }
}

